package com.ruoyi.web.controller.obs.service;

import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectResult;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.obs.config.ObsProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@Service
public class ObsService {

    /**
     * 特别说明:
     * application/msword 对应 .doc
     * application/vnd.openxmlformats-officedocument.wordprocessingml.document 对应 .docx
     */
    private static final List<String> CONTENT_TYPES = Arrays.asList("image/jpeg", "image/gif", "image/png", "application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    private static final Logger LOGGER = LoggerFactory.getLogger(ObsService.class);

    @Autowired
    private ObsProperties obsProperties;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    public AjaxResult upload(MultipartFile file) throws IOException {

        String contype = file.getContentType();
        System.out.println(contype);
        String originalFilename = file.getOriginalFilename();

        AjaxResult ajax = AjaxResult.success();

        if (!CONTENT_TYPES.contains(contype)) {
            LOGGER.info("文件类型出错" + originalFilename);
            return null;
        } else {
            InputStream in = null;
            ObsClient obsClient = null;

            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if (bufferedImage != null) {
                LOGGER.info("上传成功的图片名称为" + originalFilename);
                //上传图片
                //获取图片名称，作为上传文件名参数
                String objectKey = file.getOriginalFilename();
                //获取流对象
                in = file.getInputStream();
                // 创建ObsClient实例
                obsClient = new ObsClient(obsProperties.getAk(), obsProperties.getSk(), obsProperties.getEndPoint());
                // 使用访问OBS
                PutObjectResult putObjectResult = obsClient.putObject("tlgenhuawei", objectKey, in);
                //将图片信息封装起来，方便前端回显调用
                String url = putObjectResult.getObjectUrl();

                ajax.put("name", objectKey);
                ajax.put("url", url);
                return ajax;
            } else {
                //获取文件名称，作为上传文件名参数
                String objectKey = file.getOriginalFilename();
                //获取流对象
                in = file.getInputStream();
                // 创建ObsClient实例
                obsClient = new ObsClient(obsProperties.getAk(), obsProperties.getSk(), obsProperties.getEndPoint());
                // 使用访问OBS
                PutObjectResult putObjectResult = obsClient.putObject("tlgenhuawei", objectKey, in);
                //将图片信息封装起来，方便前端回显调用
                String url = putObjectResult.getObjectUrl();

                System.out.println("华为obs上传成功预览的url为: " + url);

                ajax.put("name", objectKey);
                ajax.put("url", url);

                in.close();

                obsClient.close();

                return ajax;
            }

        }

    }
}

